﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OnlineShop
{
    /// <summary>
    /// Interaction logic for AddProductMenu.xaml
    /// </summary>
    public partial class AddProductMenu : Window
    {
        public AddProductMenu()
        {
            InitializeComponent();
        }

        private void OnAddProductClick(object sender, RoutedEventArgs e)
        {
            this.name.Text = this.name.Text.Trim();
            this.brand.Text = this.brand.Text.Trim();
            this.cost.Text = this.cost.Text.Trim();

            if (this.name.Text == "")
            {
                MessageBox.Show("Name should not be null !");
                return;
            }
            if (this.brand.Text == "")
            {
                MessageBox.Show("Brand should not be null !");
                return;
            }
            if (this.cost.Text == "")
            {
                MessageBox.Show("Cost should not be null !");
                return;
            }

            Product currProduct = new Product();
            try
            {
                currProduct.Name = this.name.Text;
                currProduct.Brand = this.brand.Text;
                currProduct.Cost = decimal.Parse(this.cost.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("Cost must be number");
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            if (File.Exists("Products\\" + currProduct.Name + ".txt"))
            {
                MessageBox.Show(string.Format("Product {0} already exists", currProduct.Name));
                return;
            }
            StreamWriter writer = new StreamWriter("Products\\" + currProduct.Name + ".txt");

            using (writer)
            {
                writer.WriteLine("Name:{0}:", currProduct.Name);
                writer.WriteLine("Brand:{0}:", currProduct.Brand);
                writer.WriteLine("Cost:{0}:", currProduct.Cost);
                MessageBox.Show("Registered succesfully");
            }
        }

        private void OnBackToShopMenuClick(object sender, RoutedEventArgs e)
        {
            ShopMenu window = new ShopMenu();
            window.Show();
            this.Close();
        }
    }
}
